package org.project.choujiang.controller;

import cn.hutool.core.util.ObjectUtil;
import org.project.choujiang.mapper.UserRepository;
import org.project.choujiang.utils.TokenUtils;
import org.project.choujiang.pojo.domain.User;
import org.project.choujiang.pojo.ro.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping(value ="login")
@RestController
public class LoginController {
    @Autowired
    private UserRepository userMapper;

    @PostMapping("login")
    public R<String> login(@RequestBody Map<String,String> param){
        User user = userMapper.findByUsername(param.get("username"));
        if (ObjectUtil.isNotNull(user) && user.getPassword().equals(param.get("password"))){
            return R.success(TokenUtils.login(user));
        }

        return R.fail("登录失败");
    }

    @GetMapping(value ="getUserInfo")
    public R<User> getUserInfo(){
        User loginUserInfo = TokenUtils.getLoginUserInfo();
        loginUserInfo.setPassword(null);
        return R.success(loginUserInfo);
    }
}




